package com.frxuexi.park2209_2.video.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209_2.R
import com.frxuexi.park2209_2.databinding.ActivityVideoBinding
import com.frxuexi.park2209_2.video.adapter.VideoAdapter
import com.frxuexi.park2209_2.video.viewmodel.VideoViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class VideoActivity : BaseActivity<ActivityVideoBinding,VideoViewModel>() {
    val videoAdapter by lazy { VideoAdapter() }

    override fun initData() {
        vm.videosSuc.observe(this){
            videoAdapter.addData(it)
            videoAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.rvVideo.apply {
            adapter=videoAdapter
            layoutManager=StaggeredGridLayoutManager(2,RecyclerView.VERTICAL)
        }
        videoAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/VideoDetailActivity").withSerializable("video",videoAdapter.data.get(position)).navigation()
        }
        binding.etSearch.setOnClickListener {
            ARouter.getInstance().build("/app/VideoSearchActivity").navigation()
        }

    }

    override fun onResume() {
        super.onResume()
        videoAdapter.data.clear()
        vm.getVideos(1)
    }

}